package com.example.demo.base64;

import org.apache.tomcat.util.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.IOException;

public class LhlBase64 {

    public static String src = "lhlong fuck PMO";

    public static void main(String[] args){
        jdkBase64();
        commonsCodeBase64();
        bouncyCastleBase64();
    }

    /**
     * 使用jdk内置方法实现
     */
    public static void jdkBase64(){
        BASE64Encoder encoder = new BASE64Encoder();
        String encode = encoder.encode(src.getBytes());
        System.out.println(encode);
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            System.out.println(new String(decoder.decodeBuffer(encode)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用commons code实现
     */
    public static void commonsCodeBase64(){
        byte[] encodeBytes = Base64.encodeBase64(src.getBytes());
        System.out.println(new String(encodeBytes));

        byte[] decodeBytes = Base64.decodeBase64(encodeBytes);
        System.out.println(new String(decodeBytes));
    }

    /**
     * 使用BC实现
     */
    public static void bouncyCastleBase64(){
        byte[] encodeBytes = org.bouncycastle.util.encoders.Base64.encode(src.getBytes());
        System.out.println(new String(encodeBytes));

        byte[] decodeByte = org.bouncycastle.util.encoders.Base64.decode(encodeBytes);
        System.out.println(new String(decodeByte));
    }
}
